﻿namespace WMS6.BLL
{
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Data.Common;
    using System.Data.SqlClient;
    using WMS6.Common;
    using WMS6.DataEntity;
    using WMS6.DBUtility;
    using WMS6.IDAL;
    using WMS6.QueryEntity;
    using WMS6.DALFactory;

    public class RoleFunctionBLL
    {
        private static readonly ICommonMethodDA commonMethod = DataAccess.CreateCommonMethod();
        private List<string> dataBaseID;
        private static readonly IRoleDA RoleDA = DataAccess.CreateRoleDA();
        private static readonly IRoleFunctionDA RoleFunctionDA = DataAccess.CreateRoleFunctionDA();

        public RoleFunctionBLL()
        {
            this.dataBaseID = new List<string> { "SYSADM." };
        }

        public RoleFunctionBLL(string currentWH)
        {
            if (!string.IsNullOrEmpty(currentWH))
            {
                this.dataBaseID = commonMethod.GetDataBaseID(currentWH);
                if (this.dataBaseID == null)
                {
                    PublicMethod.GetInstance().ShowAlertMessage("C001");
                }
            }
            else
            {
                PublicMethod.GetInstance().ShowAlertMessage("C002");
            }
        }

        public string DeleteRoleAndRoleFunction(List<string> roleID, PubParasInfo pubParasInfo)
        {
            DataBase db = new DataBase();
            DbConnection conn = db.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            string strRole = string.Empty;
            try
            {
                RoleFunctionDA.DeleteRoleOperation(db, tran, roleID);
                RoleFunctionDA.DeleteRoleFunction(db, tran, roleID);
                if (RoleDA.DeleteRole(db, tran, roleID, this.dataBaseID) > 0)
                {
                    for (int i = 0; roleID.Count > i; i++)
                    {
                        strRole = strRole + roleID[i] + ",";
                    }
                }
                tran.Commit();
            }
            catch (Exception ex)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(ex, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            if (!string.IsNullOrEmpty(strRole))
            {
                strRole = strRole.Substring(0, strRole.Length - 1) + PublicMethod.GetInstance().GetGlobalMsgResource("F110_003", pubParasInfo.UserLanguage);
            }
            return strRole;
        }

        public DataSet GetAllFunctionList()
        {
            return RoleFunctionDA.GetAllFunctionList();
        }

        public List<string> GetOperationByUserIDAndFunctionID(string userID, string functionID, PubParasInfo pubParasInfo)
        {
            DataSet ds = RoleFunctionDA.GetOperationByUserIDAndFunctionID(userID, functionID);
            List<string> operations = null;
            if ((ds != null) && (ds.Tables[0].Rows.Count > 0))
            {
                operations = new List<string>();
                for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                {
                    operations.Add(ds.Tables[0].Rows[i]["BUTTON_NAME"].ToString());
                }
            }
            return operations;
        }

        public DataSet GetRoleAndRoleFunctionDataByRoleId(string roleId, PubParasInfo pubParasInfo)
        {
            return RoleFunctionDA.GetRoleAndRoleFunctionDataByRoleId(roleId);
        }

        public DataSet GetRoleFunctionData(string id, string type, PubParasInfo pubParasInfo)
        {
            return RoleFunctionDA.GetRoleFunctionData(id, pubParasInfo.UserLanguage, type);
        }

        public DataSet GetRoleMenuData(string roleID, string currentWH, string isShowQuery, PubParasInfo pubParasInfo)
        {
            return RoleFunctionDA.GetRoleMenuData(roleID, pubParasInfo.UserLanguage, isShowQuery, currentWH);
        }

        public DataSet GetWHDataByUserID(string userID, PubParasInfo pubParasInfo)
        {
            return RoleFunctionDA.GetWHDataByUserID(userID);
        }

        public string UpdateRoleFunctionAndOperationByRoleGID(string Type, RoleInfo roleInfo, List<string> FunctionTrueList, List<string> FunctionFalseList, List<string> OperationTrueList, List<string> OperationFalseList, string strCopyName, PubParasInfo pubParasInfo)
        {
            DataBase db = new DataBase();
            string RoleID = string.Empty;
            DbConnection conn = db.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            try
            {
                int i;
                RoleInfo role = roleInfo;
                role.UpdatedBy = role.CreatedBy;
                string CS40001 = Type;
                if (CS40001 != null)
                {
                    if (!(CS40001 == "0"))
                    {
                        if (CS40001 == "1")
                        {
                            goto Label_00E5;
                        }
                    }
                    else if (!RoleDA.CheckRoleIDUnique(role.RoleID, this.dataBaseID[0]))
                    {
                        if (RoleDA.InsertRole(role, this.dataBaseID) > 0)
                        {
                            RoleID = role.RoleID;
                        }
                    }
                    else
                    {
                        List<string> gInitLocal1 = new List<string> {
                            role.RoleID
                        };
                        PublicMethod.GetInstance().ShowAlertMessage("F1100_001", pubParasInfo.UserLanguage, gInitLocal1);
                    }
                }
                goto Label_015D;
            Label_00E5:
                if (RoleDA.CheckRoleIDUnique(role.RoleID, this.dataBaseID[0]))
                {
                    RoleDA.UpdateRole(role, this.dataBaseID);
                    RoleID = role.RoleID;
                }
                else
                {
                    List<string> gInitLocal2 = new List<string> {
                        role.RoleID
                    };
                    PublicMethod.GetInstance().ShowAlertMessage("F1100_002", pubParasInfo.UserLanguage, gInitLocal2);
                }
            Label_015D:
                if (!string.IsNullOrEmpty(strCopyName))
                {
                    RoleDA.RoleCopy(db, tran, strCopyName, roleInfo);
                }
                RoleFunctionDA.DeleteRoleFunction(db, tran, FunctionFalseList, role.RoleID);
                RoleFunctionDA.DeleteRoleFunction(db, tran, FunctionTrueList, role.RoleID);
                List<RoleFunctionInfo> roleFunctionTrueList = new List<RoleFunctionInfo>();
                for (i = 0; i < FunctionTrueList.Count; i++)
                {
                    RoleFunctionInfo roleFunctionTrue = new RoleFunctionInfo {
                        CreatedBy = role.RoleID,
                        UpdatedBy = role.RoleID,
                        FunctionID = FunctionTrueList[i].ToString(),
                        RoleID = role.RoleID
                    };
                    RoleFunctionDA.InsertRoleFunction(db, tran, roleFunctionTrue);
                }
                RoleFunctionDA.DeleteRoleOperation(db, tran, OperationFalseList, role.RoleID);
                RoleFunctionDA.DeleteRoleOperation(db, tran, OperationTrueList, role.RoleID);
                List<RoleOperationInfo> roleOperationTrueList = new List<RoleOperationInfo>();
                for (i = 0; i < OperationTrueList.Count; i++)
                {
                    RoleOperationInfo roleOperationTrue = new RoleOperationInfo {
                        CreatedBy = role.RoleID,
                        UpdatedBy = role.RoleID,
                        OperationID = OperationTrueList[i].ToString(),
                        RoleID = role.RoleID
                    };
                    RoleFunctionDA.InsertRoleOperation(db, tran, roleOperationTrue);
                }
                tran.Commit();
                if (Type == "0")
                {
                    return RoleID;
                }
            }
            catch (SqlException sqlEx)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(sqlEx, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            return null;
        }
    }
}

